package com.diamond.service.impl;

import java.util.List;

import com.diamond.bo.UserFunctionBO;
import com.diamond.entity.TUserFunction;
import com.diamond.po.UserFunctionPO;
import com.diamond.service.IUserFunctionService;
import com.diamond.util.StringUtil;

/**
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked") 
public class UserFunctionService extends Service<UserFunctionPO> implements IUserFunctionService {

	@Override
	public Boolean delUserFunction(String userId, String[] functionCodes)
			throws Exception {
		Boolean result = false;
		if (functionCodes.length > 0 && !StringUtil.isEmpty(userId)) {
			StringBuilder functionCode = new StringBuilder();
			for (int i = 0; i < functionCodes.length; i++) {
				if (i<functionCodes.length-1) {
					functionCode.append(functionCodes[i]).append(",");
				} else {
					functionCode.append(functionCodes[i]);
				}
			}
			String sql = "update t_user_function uf set uf.activ_status='N' where uf.user_id="+userId+" and uf.function_code in ("+functionCode.toString()+")";
			Integer i = super.genericDAO.updateBySql(sql);
			if (i>0) {
				result = true;
			}
		}
		return result;
	}

	@Override
	public List<UserFunctionBO> queryUserFunction(String userId) throws Exception {
		String sql = "select * from t_user_function uf, t_base_function bf where uf.function_code=bf.function_code and uf.user_id="+userId+" and uf.activ_status='Y'";
		List<UserFunctionBO> list = super.genericDAO.findBySqlToObj(sql, UserFunctionBO.class);
		return list;
	}

	@Override
	public Boolean saveOrUpdateUserFunction(String userId, List<UserFunctionBO> userFunctionBolist)
			throws Exception {
		Boolean result = false;
		if (!StringUtil.isEmpty(userId)) {
			String delSql = "delete from t_user_function where t_user_function.user_id="+userId;
			super.genericDAO.updateBySql(delSql);
			for (UserFunctionBO userFunctionBo : userFunctionBolist) {
				TUserFunction tUserFunction = new TUserFunction();
				tUserFunction.setUserId(userFunctionBo.getUserId());
				tUserFunction.setFunctionCode(userFunctionBo.getFunctionCode());
				super.genericDAO.save(tUserFunction);
			}
			result = true;
		}
		return result;
	}
 

}
